package com.zthz.douxing.event

import com.zthz.douxing.TaskType

import javax.annotation.PostConstruct
import java.util.concurrent.Callable
import java.util.concurrent.Executors

/**
 * 用于从redis的工作获取工作列表。用于转换成特定的任务。
 */
class TaskAcceptService {

    //引用的服务
    def eventBus
    def redisService
    def gsonBuilder
    def redisKeyGenernateService


    def eventClass
    TaskType taskType

    public TaskAcceptService(TaskType taskType, Class clazz){
        this.taskType = taskType
        this.eventClass = clazz
    }

    @PostConstruct
    def init(){
        def pool = Executors.newFixedThreadPool(1)
        def gson = gsonBuilder.create()

        def eventAccepterThead = {
            while(true){
                //事件抓取泵
                try{
                    def taskBody = redisService.getTaskFromQueue(redisKeyGenernateService.getRedisKey(taskType.name(), null))
                    if(taskBody){
                        def taskEvent = gson.fromJson(taskBody, eventClass)
                        eventBus.post(taskEvent)
                    } else {
                        
                    }
                } catch (Exception e){

                }

            }
        } as Callable
        pool.submit(eventAccepterThead)
    }

}
